<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单位名称" prop="unitName" label-width="150px">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="组织机构代码"
        prop="organizationCode"
        label-width="150px"
      >
        <el-input
          v-model="queryParams.organizationCode"
          placeholder="请输入组织机构代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="社会信用代码"
        prop="socialCreditCode"
        label-width="150px"
      >
        <el-input
          v-model="queryParams.socialCreditCode"
          placeholder="请输入社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['training:units:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['training:units:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['training:units:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['training:units:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- <el-table
      v-loading="loading"
      :data="unitsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        width="50"
        fixed="left"
        align="center"
      />
      <el-table-column label="单位名称" align="center" prop="unitName" />
      <el-table-column
        label="法定代表人"
        align="center"
        prop="legalRepresentative"
      />
      <el-table-column
        label="组织机构代码"
        align="center"
        prop="organizationCode"
      />
      <el-table-column
        label="社会信用代码"
        align="center"
        prop="socialCreditCode"
      />
      <el-table-column
        label="行政规划"
        align="center"
        prop="administrativeArea"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pxzx_xzqh"
            :value="scope.row.administrativeArea"
          />
        </template>
      </el-table-column>
      <el-table-column label="邮政编码" align="center" prop="postCode" />
      <el-table-column label="手机号码" align="center" prop="unitPhonenumber" />
      <el-table-column label="用户邮箱" align="center" prop="email" />
      <el-table-column label="传真" align="center" prop="fax" />
      <el-table-column label="网站" align="center" prop="website" />
      <el-table-column label="联系地址" align="center" prop="address" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['training:units:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['training:units:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table> -->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="unitsList"
        @selection-change="handleSelectionChange"
        fit
        :header-cell-style="{
          'background-color': '#f5f7fa',
          'font-weight': 'bold',
          padding: '10px 0',
          border: 'none',
        }"
        :cell-style="{
          padding: '10px 0',
          border: 'none',
          'white-space': 'nowrap', // 强制内容不换行
        }"
        :row-style="{ hover: '#f5f7fa' }"
      >
        <!-- 选择列 -->
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />

        <!-- 序号列 -->
        <el-table-column
          label="序号"
          type="index"
          width="60"
          fixed="left"
          align="center"
        />

        <!-- 所有列使用min-width确保在一行显示 -->
        <el-table-column
          label="单位名称"
          align="center"
          prop="unitName"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="法定代表人"
          align="center"
          prop="legalRepresentative"
          min-width="120"
        />
        <el-table-column
          label="组织机构代码"
          align="center"
          prop="organizationCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="社会信用代码"
          align="center"
          prop="socialCreditCode"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="行政规划"
          align="center"
          prop="administrativeArea"
          min-width="120"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.pxzx_xzqh"
              :value="scope.row.administrativeArea"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="邮政编码"
          align="center"
          prop="postCode"
          min-width="100"
        />
        <el-table-column
          label="联系人"
          align="center"
          prop="contact"
          min-width="130"
        />
        <el-table-column
          label="手机号码"
          align="center"
          prop="unitPhonenumber"
          min-width="130"
        />
        <el-table-column
          label="用户邮箱"
          align="center"
          prop="email"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="传真"
          align="center"
          prop="fax"
          min-width="130"
        />
        <el-table-column
          label="网站"
          align="center"
          prop="website"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="联系地址"
          align="center"
          prop="address"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          min-width="180"
          show-overflow-tooltip
        />

        <!-- 操作列 -->
        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          min-width="120"
          class-name="table-operation"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['training:units:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['training:units:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单位对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="legalRepresentative">
          <el-input
            v-model="form.legalRepresentative"
            placeholder="请输入法定代表人"
          />
        </el-form-item>
        <el-form-item label="组织机构代码" prop="organizationCode">
          <el-input
            v-model="form.organizationCode"
            placeholder="请输入组织机构代码"
          />
        </el-form-item>
        <el-form-item label="社会信用代码" prop="socialCreditCode">
          <el-input
            v-model="form.socialCreditCode"
            placeholder="请输入社会信用代码"
          />
        </el-form-item>
        <el-form-item label="行政规划" prop="administrativeArea">
          <el-select
            v-model="form.administrativeArea"
            placeholder="请选择行政规划"
          >
            <el-option
              v-for="dict in dict.type.pxzx_xzqh"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮政编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入邮政编码" />
        </el-form-item>
        <el-form-item label="手机号码" prop="unitPhonenumber">
          <el-input v-model="form.unitPhonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="传真" prop="fax">
          <el-input v-model="form.fax" placeholder="请输入传真" />
        </el-form-item>
        <el-form-item label="网站" prop="website">
          <el-input v-model="form.website" placeholder="请输入网站" />
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入联系地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <div
        class="form-container"
        style="max-height: 600px; overflow-y: auto; padding-right: 10px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <!-- 基本信息分组 -->
          <div class="form-group">
            <h3 class="group-title">基本信息</h3>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="单位名称" prop="unitName">
                  <el-input
                    v-model="form.unitName"
                    placeholder="请输入单位名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法定代表人" prop="legalRepresentative">
                  <el-input
                    v-model="form.legalRepresentative"
                    placeholder="请输入法定代表人"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="组织机构代码" prop="organizationCode">
                  <el-input
                    v-model="form.organizationCode"
                    placeholder="请输入组织机构代码"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="社会信用代码" prop="socialCreditCode">
                  <el-input
                    v-model="form.socialCreditCode"
                    placeholder="请输入社会信用代码"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="行政规划" prop="administrativeArea">
                  <el-select
                    v-model="form.administrativeArea"
                    placeholder="请选择行政规划"
                  >
                    <el-option
                      v-for="dict in dict.type.pxzx_xzqh"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="邮政编码" prop="postCode">
                  <el-input
                    v-model="form.postCode"
                    placeholder="请输入邮政编码"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </div>

          <!-- 联系方式分组 -->
          <div class="form-group">
            <h3 class="group-title">联系方式</h3>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="联系人" prop="contact">
                  <el-input
                    v-model="form.contact"
                    placeholder="请输入联系人"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="手机号码" prop="unitPhonenumber">
                  <el-input
                    v-model="form.unitPhonenumber"
                    placeholder="请输入手机号码"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="用户邮箱" prop="email">
                  <el-input v-model="form.email" placeholder="请输入用户邮箱" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="传真" prop="fax">
                  <el-input v-model="form.fax" placeholder="请输入传真" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="网站" prop="website">
                  <el-input v-model="form.website" placeholder="请输入网站" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="联系地址" prop="address">
                  <el-input
                    v-model="form.address"
                    placeholder="请输入联系地址"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </div>

          <!-- 其他信息分组 -->
          <div class="form-group">
            <h3 class="group-title">其他信息</h3>
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                rows="3"
              />
            </el-form-item>
          </div>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUnits,
  getUnits,
  delUnits,
  addUnits,
  updateUnits,
} from "@/api/training/units";

export default {
  name: "Units",
  dicts: ["pxzx_xzqh"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单位表格数据
      unitsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        organizationCode: null,
        socialCreditCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询单位列表 */
    getList() {
      this.loading = true;
      listUnits(this.queryParams).then((response) => {
        this.unitsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        unitId: null,
        unitName: null,
        legalRepresentative: null,
        organizationCode: null,
        socialCreditCode: null,
        administrativeArea: null,
        postCode: null,
        unitPhonenumber: null,
        email: null,
        fax: null,
        website: null,
        address: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isTrace: null,
        contact: null,
        exampleOne: null,
        exampleTwo: null,
        exampleThree: null,
        delFlag: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.unitId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const unitId = row.unitId || this.ids;
      getUnits(unitId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.unitId != null) {
            updateUnits(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnits(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const unitIds = row.unitId || this.ids;
      this.$modal
        .confirm('是否确认删除单位编号为"' + unitIds + '"的数据项？')
        .then(function () {
          return delUnits(unitIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "training/units/export",
        {
          ...this.queryParams,
        },
        `units_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
/* 容器设置横向滚动 */
.table-container {
  overflow-x: auto;
  padding-bottom: 8px; /* 给滚动条留出空间 */
}

/* 优化滚动条样式 */
.table-container::-webkit-scrollbar {
  height: 8px;
}

.table-container::-webkit-scrollbar-thumb {
  border-radius: 4px;
  background-color: #ddd;
  transition: background-color 0.2s;
}

.table-container::-webkit-scrollbar-thumb:hover {
  background-color: #bbb;
}

.table-container::-webkit-scrollbar-track {
  background-color: #f5f5f5;
  border-radius: 4px;
}

/* 操作列样式 */
.table-operation .el-button + .el-button {
  margin-left: 8px;
}

.table-operation .el-button {
  transition: color 0.2s;
}

.table-operation .el-button:hover {
  color: #1890ff;
}

/* 行悬停效果 */
.el-table__row:hover > td {
  background-color: #fafafa !important;
}

/* 彻底去除所有边框痕迹 */
.el-table::before,
.el-table__fixed-right::before,
.el-table__fixed::before {
  display: none !important;
}

/* 修复固定列与内容区的视觉衔接 */
.el-table__fixed,
.el-table__fixed-right {
  box-shadow: none !important;
}

.form-group {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px dashed #e5e6eb;
}

.form-group:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.group-title {
  margin-bottom: 15px;
  padding-left: 5px;
  font-size: 15px;
  color: #1890ff;
  border-left: 3px solid #1890ff;
}
</style>
